package com.hby.game

import android.graphics.Bitmap
import android.view.MotionEvent
import game.engine.base.*
import game.engine.base.anim.single.MoveTo
import game.engine.base.anim.call
import game.engine.base.anim.scaleTo

class GameScene : Scene() {

    var allGameEnd = false
    var elapsedTime = 0f
    var hbCreatorEnd = false

    val lstHb = ArrayList<HbSprite>()

    lateinit var lbCoin: Label
    lateinit var lbTime: Label
    lateinit var spCoin: Sprite

    var coins = 0
    var coinPos = Vec2()

    var hbIdx = 0

    override fun onTouch(touch: Touch) {
        if (touch.type == MotionEvent.ACTION_DOWN) {

//            UDebug.info("touch === x:${touch.x},y:${touch.y}")

            // 判断哪个被点击了
            // 需要分层级,先检查后面的
            for (i in lstHb.size - 1 downTo 0) {
                val hb = lstHb[i]
                if (hb.getBoundingBox().contains(touch.x, touch.y)) {
                    hb.removeFromParent()
                    lstHb.removeAt(i)

                    coins += hb.coin
                    lbCoin.text = "$coins"

                    // 一个金币飞过去
                    val graphics = HbyGame.res
                    val coin = Sprite(bitmap = graphics.coinBitmap).apply {
                        x = touch.x
                        y = touch.y
                        zorder = 1
                    }
                    addChild(coin)

                    MoveTo(0.5f, coinPos.x, coinPos.y)
                            .scaleTo(0.2f, 1.2f, 1.2f)
                            .scaleTo(0.2f, 1.0f, 1.0f)
                            .call {
                                coin.removeFromParent()
                            }.runBy(coin)

                    return
                }
            }
        }
    }

    override fun onEnter() {
        super.onEnter()

        // 显示一个全局的背景
        //val director = Director.instance
        val graphics = HbyGame.res
        fillBgBitmap = graphics.bgBitmap

        // info
        val coinInfo = HbConfig.makeCoinInfo()
        addChild(coinInfo)
        spCoin = coinInfo.getChildByTag(HbTags.CoinIcon) as Sprite
        lbCoin = coinInfo.getChildByTag(HbTags.CoinText) as Label

        val timeInfo = HbConfig.makeTimeInfo()
        addChild(timeInfo)
        lbTime = timeInfo.getChildByTag(HbTags.TimeText) as Label

        // 进行10s 开始倒计时
        val count = 10
        scheduleCount(1f, count, 1f) { countIdx ->
            if (countIdx >= count - 1) {
                hbCreatorEnd = true
            }
            lbTime.text = "倒计时:${count - countIdx - 1}s"
        }

        coinPos = spCoin.getWorldPos()
        UDebug.info("coinPos:${coinPos.x},${coinPos.y}")
    }

    override fun update(dt: Float) {

        if (allGameEnd) return

        // 先更新
        val director = Director.instance
        val wa = director.windowAdapter
        val iter = lstHb.iterator()
        while (iter.hasNext()) {
            val hb = iter.next()
            hb.y += hb.speed * dt
            if (hb.y >= wa.maxY + hb.height / 2f) {
                iter.remove()
            }
        }

        if (hbCreatorEnd && lstHb.size < 1) {
            // 弹出结果框
            allGameEnd = true
            director.gameApp.showResult(coins)
        }

        // 产生一堆红包
        if (!hbCreatorEnd) {
            elapsedTime += dt
            if (elapsedTime >= 0.25f) {
                elapsedTime = 0f
                // 产生一个红包
                val hb = genHb()
                addChild(hb)
                lstHb.add(hb)
            }
        }
    }

    private fun genHb(): HbSprite {
        // 第8 和 18个 出现金色的
        val bitmap: Bitmap
        val coin: Int
        if (hbIdx == 7 || hbIdx == 17) {
            bitmap = HbyGame.res.hbBitmap2
            coin = genBigCoin()
        } else {
            bitmap = HbyGame.res.hbBitmap1
            coin = 2
        }
        hbIdx++
        val speed = CoreUtil.randomFloat(600f, 1600f)
        val wa = Director.instance.windowAdapter
        return HbSprite(bitmap = bitmap, speed, coin).apply {
            x = CoreUtil.randomFloat(wa.minX + width / 2f + 30f, wa.maxX - width / 2f - 30f)
            y = wa.minY + height / 2f + HbConfig.InfoOffsetY
        }
    }

    private fun genBigCoin(): Int {
        val value = CoreUtil.randomInt(0, 99)
        return when {
            value < 70 -> 18
            value < 90 -> 28
            value < 96 -> 38
            else -> 48
        }
    }
}